

package com.zy.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import java.util.HashMap;
import java.util.Map;

public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public R() {
        this.put((String)"code", 0);
        this.put((String)"msg", "success");
    }

    public <T> T getData(String key, TypeReference<T> typeReference) {
        Object data = this.get(key);
        return JSON.parseObject(JSON.toJSONString(data), typeReference, new Feature[0]);
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public <T> T getData(TypeReference<T> typeReference) {
        Object data = this.get("data");
        String s = JSON.toJSONString(data);
        return JSON.parseObject(s, typeReference, new Feature[0]);
    }

    public R setData(Object data) {
        this.put("data", data);
        return this;
    }

    public static R error() {
        return error(1, "未知异常");
    }

    public static R error(String msg) {
        return error(1, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put((String)"code", code);
        r.put((String)"msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put((String)"msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    public Integer getCode() {
        return (Integer)this.get("code");
    }
}
